<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品编码" prop="ITMREF">
        <el-input v-model="queryParams.ITMREF" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="C2">
        <el-input v-model="queryParams.C2" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['x3base:Gesitm2:add']" >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="Gesitm2List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品编码" align="center" prop="ITMREF" />
      <el-table-column label="产品名称" align="center" prop="C2" />
      <el-table-column label="产品简称" align="center" prop="C3" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['x3base:Gesitm2:edit']" v-if="scope.row.ITMSTA<=1">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['x3base:Gesitm2:remove']" v-if="scope.row.ITMSTA<=1">删除</el-button>
          <el-button size="mini" type="text" @click="handle('a',scope.row)" v-if="scope.row.ITMSTA==1">审核</el-button>
          <el-button size="mini" type="text" @click="handle('b',scope.row)" v-if="scope.row.ITMSTA==2">停用</el-button>
          <el-button size="mini" type="text" @click="handle('c',scope.row)" v-if="scope.row.ITMSTA==2">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" inline>
        <el-form-item label="产品种类" prop="TCLCOD">
          <el-input v-model="form.TCLCOD" placeholder="请输入产品种类"/>
        </el-form-item>
        <el-form-item label="产品编码" prop="ITMREF">
          <el-input v-model="form.ITMREF" placeholder="请输入产品编码" :disabled="isAdd!=true"/>
        </el-form-item>
        <el-form-item label="产品名称" prop="DES1AXX">
          <el-input v-model="form.DES1AXX" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="状态" prop="YSTA">
          <el-select v-model="form.YSTA" @change="(val) => change('YSTA',val, form)" filterable>
            <el-option v-for="dict in dict.type.x3_1003" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与预测" prop="YCYYC">
          <el-select v-model="form.YCYYC" @change="(val) => change('YCYYC',val, form)" filterable>
            <el-option v-for="dict in dict.type.x3_1" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-tabs v-model="activeName">
          <el-tab-pane label="基本信息" name="1">
            <el-form-item label="产品简称" prop="DES2AXX"><el-input v-model="form.DES2AXX" placeholder="请输入产品简称" /></el-form-item>
            <el-form-item label="产品保质期" prop="YBZQ"><el-input v-model="form.YBZQ" placeholder="请输入产品保质期" /></el-form-item>
            <el-form-item label="产品执行标准" prop="YZXBZ"><el-input v-model="form.YZXBZ" placeholder="请输入产品执行标准" /></el-form-item>
            <el-form-item label="克重" prop="YZL"><el-input v-model="form.YZL" placeholder="请输入克重" /></el-form-item>
            <el-form-item label="箱规" prop="YXG"><el-input v-model="form.YXG" placeholder="请输入箱规" /></el-form-item>
            <el-form-item label="段位" prop="YDW"><el-input v-model="form.YDW" placeholder="请输入段位" /></el-form-item>
            <el-form-item label="产品标准价格" prop="YBZJG"><el-input v-model="form.YBZJG" placeholder="请输入产品标准价格" /></el-form-item>
            <el-form-item label="标准零售价" prop="YLSJ"><el-input v-model="form.YLSJ" placeholder="请输入标准零售价" /></el-form-item>
            <el-form-item label="红线价" prop="YHXJ"><el-input v-model="form.YHXJ" placeholder="请输入红线价" /></el-form-item>
            <el-form-item label="经销商结算价" prop="YJSSJSJ"><el-input v-model="form.YJSSJSJ" placeholder="请输入经销商结算价" /></el-form-item>
            <el-form-item label="事业部结算价" prop="YSYBJSJ"><el-input v-model="form.YSYBJSJ" placeholder="请输入事业部结算价" /></el-form-item>
            <el-form-item label="电商结算价" prop="YDSJSJ"><el-input v-model="form.YDSJSJ" placeholder="请输入电商结算价" /></el-form-item>
            <el-form-item label="直营客户结算价" prop="YZYJSJ"><el-input v-model="form.YZYJSJ" placeholder="请输入直营客户结算价" /></el-form-item>
            <el-form-item label="版本说明" prop="YBBSM"><el-input v-model="form.YBBSM" placeholder="请输入版本说明" /></el-form-item>
            <el-form-item label="库存计量单位" prop="STU"><el-input v-model="form.STU" placeholder="请输入库存计量单位" /></el-form-item>
          </el-tab-pane>
          <el-tab-pane label="统计组" name="2">
            <el-form-item label="所属系列" prop="TSICOD1"><el-input v-model="form.TSICOD1" placeholder="请输入所属系列" /></el-form-item>
            <el-form-item label="所属小系列" prop="TSICOD2"><el-input v-model="form.TSICOD2" placeholder="请输入所属小系列" /></el-form-item>
          </el-tab-pane>
          <el-tab-pane label="销售/ERP品号对应" name="3">
            <el-table :data="form.detail" border>
              <el-table-column label="ERP产品编码">
                <template slot-scope="scope">
                  <el-form-item :prop="'detail.' + scope.$index + '.YERPITMREF'" :rules="rules.YERPITMREF">
                    <el-input v-model="scope.row.YERPITMREF" placeholder="请输入" clearable></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="是否最新">
                <template slot-scope="scope">
                  <el-form-item :prop="'detail.' + scope.$index + '.YSFZX'">
                    <el-select v-model="scope.row.YSFZX" @change="(val) => linechange('YSFZX',val, scope.row)" filterable>
                      <el-option v-for="dict in dict.type.x3_1" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="80" fixed="right">
                <template slot-scope="scope">
                  <el-button type="text" v-if="scope.$index != 0" @click="del(scope.$index)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="add" @click="add">
              <i class="el-icon-plus" style="font-size: 18px; font-weight: 600; margin-right: 10px"></i>
              添加
            </div>

          </el-tab-pane>
          <el-tab-pane label="图片/附件" name="4">
            <el-form-item label="最新产品展开图：" style="width:400px" prop="attachment">
              <Upload :value="imgList" :limit="1"  @input="(val) => setImg(val,1)"></Upload>
            </el-form-item>
            <el-form-item label="最新产品效果图：" style="width:400px" prop="attachment">
              <Upload :value="imgList2" :limit="1"  @input="(val) => setImg(val,2)"></Upload>
            </el-form-item>
            <el-form-item label="附件1：" style="width:400px" prop="attachment">
              <Upload2 :value="imgList3" :limit="1" :fileType="fileType2" @input="(val) => setImg(val,3)"></Upload2>
            </el-form-item>
            <el-form-item label="附件2：" style="width:400px" prop="attachment">
              <Upload2 :value="imgList4" :limit="1" :fileType="fileType2" @input="(val) => setImg(val,4)"></Upload2>
            </el-form-item>
            <el-form-item label="最新产品展开图" prop="IMG"><el-input v-model="form.IMG" placeholder="请输入最新产品展开图" /></el-form-item>
            <el-form-item label="最新产品效果图" prop="IMG2"><el-input v-model="form.IMG2" placeholder="请输入最新产品效果图" /></el-form-item>
            <el-form-item label="附件1" prop="YFILE1"><el-input v-model="form.YFILE1" placeholder="请输入附件1" /></el-form-item>
            <el-form-item label="附件2" prop="YFILE2"><el-input v-model="form.YFILE2" placeholder="请输入附件2" /></el-form-item>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listGesitm2, getGesitm2, delGesitm2, addGesitm2, updateGesitm2, actionGesitm2} from "@/api/x3base/gesitm2";
import Upload from "@/components/ImageUpload/index.vue";
import Upload2 from "@/components/FileUpload/index.vue";

export default {
  name: "Gesitm2",
  dicts: ['x3_1','x3_1003'],
  components: {
    Upload,Upload2
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      Gesitm2List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ITMREF: null,
        C2: null
      },
      // 表单参数
      form: {
        detail:[]
      },
      // 表单校验
      rules: {
      },
      isAdd:false,
      // 选中选项卡的 name
      activeName: "1",
      imgList: [],
      fileType: ["png", "bmp", "jpg", "jpeg"],//图片
      fileType2: [],//文件
      imgList2: [],
      imgList3: [],
      imgList4: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listGesitm2(this.queryParams).then(response => {
        this.Gesitm2List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ITMREF: null,
        DES1AXX: null,
        DES2AXX: null,
        detail:[]
      };
      this.imgList=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ITMREF)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
      this.isAdd=true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ITMREF = row.ITMREF || this.ids
      this.isAdd=false
      getGesitm2(ITMREF).then(response => {
        console.log('getGesitm2.response',response)
        this.form = response.data["HEAD"];
        this.form.TSICOD1=this.form.TSICOD[0]
        this.form.TSICOD2=this.form.TSICOD[1]
        this.form.detail = response.data["DETAIL"];
        this.open = true;
        this.title = "修改产品";
        console.log('getGesitm2.form',this.form)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isAdd != true) {
            updateGesitm2(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGesitm2(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ITMREFs = row.ITMREF || this.ids;
      this.$modal.confirm('是否确认删除产品编号为"' + ITMREFs + '"的数据项？').then(function() {
        return delGesitm2(ITMREFs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('x3base/Gesitm2/export', {
        ...this.queryParams
      }, `Gesitm2_${new Date().getTime()}.xlsx`)
    },
    /** 修改按钮操作 */
    handle(flag,row) {
      actionGesitm2(row,flag).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
    add () {
      console.log(this.form)
      this.form.detail.push({
        YERPITMREF: undefined,
        YSFZX: '2',
      })
    },
    del (index) {
      this.form.detail.splice(index, 1)
      this.$nextTick(() => {
        this.form.detail.splice(index, 1)
      })
    },
    //修改后
    linechange(fild, val, row) {
      if (fild == 'skamount') {
      }
    },
    //修改后
    change(fild, val, row) {
      if (fild == 'skamount') {
      }
    },
    setImg (data,typ) {
      console.log('setImg.data',data)
      if(typ==1){
        if(data!=''){
          this.imgList = data.split(',');
        }else{
          this.imgList = []
        }
      }
      if(typ==2){
        if(data!=''){
          this.imgList2 = data.split(',');
        }else{
          this.imgList2 = []
        }
      }
      if(typ==3){
        if(data!=''){
          this.imgList3 = data.split(',');
        }else{
          this.imgList3 = []
        }
      }
      if(typ==4){
        if(data!=''){
          this.imgList4 = data.split(',');
        }else{
          this.imgList4 = []
        }
      }

      this.form.attachment = data;//.split(',').map(item => { return { fileId: item } });
    },
  }
};
</script>

<style scoped lang="scss">
  .add {
    width: 100%;
    height: 48px;
    border: 1px dashed #479DFF;
    color: #479DFF;
    font-size: 18px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 600;
    margin-top: 20px;
    cursor: pointer;

    &:hover {
      box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
    }
  }
</style>
